package com.dashu.core.controller;

import com.dashu.base.bean.excel.ExcelStudent;
import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.enums.school.excel.ExcelFileType;
import com.dashu.base.enums.school.excel.ExcelType;
import com.dashu.core.dto.PageDTO;
import com.dashu.core.dto.company.CompanyPersonalListDTO;
import com.dashu.core.model.CompanyPersonal;
import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.base.utils.excel.ExcelUtil;
import com.dashu.core.dto.company.CompanyPersonalDTO;
import com.dashu.core.service.CompanyPersonalService;
import com.dashu.core.vo.company.AttendancePersonalListVO;
import com.dashu.core.vo.company.CompanyPersonalListVO;
import com.dashu.core.vo.company.CompanyPersonalVO;
import com.dashu.core.vo.UpdateEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/companyPersonal")
@RestController
@Slf4j
@Api(value = "dashu/companyPersonal", tags = {"组织个人模块"}, description = "后台")
public class CompanyPersonalController {

    @Autowired
    private CompanyPersonalService companyPersonalService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addPersonal")
    @ApiOperation(value = "个人添加", notes = "个人添加")
    public ResultBean<CompanyPersonalDTO> addPersonal(@RequestBody @Validated CompanyPersonalVO companyPersonalVo) {
        if (companyPersonalVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        CompanyPersonal companyPersonal = companyPersonalService.existPersonal(companyPersonalVo.getClsId(), companyPersonalVo.getNum());
        if (companyPersonal != null) {
            return new ResultBean<>(ReplRyCode.EXIST_COMPANY_PERSONAL_CLS.getCode(), ReplRyCode.EXIST_COMPANY_PERSONAL_CLS.getMessage(), null);
        }

        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "个人添加", companyPersonalService.add(companyPersonalVo,0));
    }

    @PostMapping("/addPersonals")
    @ApiOperation(value = "批量添加", notes = "批量添加")
    public ResultBean<List<CompanyPersonalDTO>> addPersonals(@RequestParam(value = "pic") MultipartFile file,long companyId, long departmentId, long clsId) throws IOException {
        String fileName = file.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf(".")).replace(".", "");
        if (companyId == 0 || departmentId == 0 || clsId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        List<ExcelStudent> excelStudents = ExcelUtil.readExcel(file.getInputStream(), ExcelFileType.getExcelFileType(fileType), ExcelType.STUDENT);
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "批量添加", companyPersonalService.batchAdd(excelStudents, companyId,departmentId,clsId));
    }

    @PostMapping("/issueBatchPersonal")
    @ApiOperation(value = "个人信息批量下发", notes = "个人信息批量下发")
    public ResultBean<Boolean> issueBatchTeacher(@RequestBody @Validated List<Long> companyPersonalIds) {
        if (companyPersonalIds == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "个人信息批量下发", companyPersonalService.issueBatchPersonal(companyPersonalIds));

    }

    @PostMapping("/updataPersonal")
    @ApiOperation(value = "个人信息修改", notes = "个人信息修改")
    public ResultBean<CompanyPersonalDTO> updataPersonal(@RequestBody @Validated UpdateEntityVO<CompanyPersonalVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "个人信息修改", companyPersonalService.updataByCompanyPersonalId(updateEntityVO));

    }

    @GetMapping("/findBase")
    @ApiOperation(value = "父节点查询个人列表", notes = "父节点查询个人列表")
    public ResultBean<List<CompanyPersonalDTO>> findBase(@RequestBody @Validated PageEnty<Long> baseVo) {
        if (baseVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询个人列表", companyPersonalService.findByCreteUser(Long.valueOf(String.valueOf(baseVo))));

    }

    @GetMapping("/findPersonal")
    @ApiOperation(value = "个人信息查询", notes = "个人信息查询")
    public ResultBean<CompanyPersonalDTO> findPersonal(long companyPersonalId) {
        if (companyPersonalId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "个人信息查询", companyPersonalService.findByCompanyPersonalId(companyPersonalId));

    }

    @DeleteMapping("/deletePersonal")
    @ApiOperation(value = "个人信息删除", notes = "个人信息删除")
    public ResultBean<Boolean> deletePersonal(long companyPersonalId) {
        if (companyPersonalId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "个人信息删除", companyPersonalService.deleteByCompanyPersonalId(companyPersonalId));

    }

    @PostMapping("/deleteBatchPersonal")
    @ApiOperation(value = "个人信息批量删除", notes = "个人信息批量删除")
    public ResultBean<Boolean> deleteBatchPersonal(@RequestBody @Validated List<Long> companyPersonalIds) {
        if (companyPersonalIds == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师信息批量删除", companyPersonalService.deleteBatchPersonal(companyPersonalIds));

    }

    @PostMapping("/pagePersonalList")
    @ApiOperation(value = "个人列表", notes = "个人列表")
    public ResultBean<PageDTO<CompanyPersonalListDTO>> pageParentList(@RequestBody @Validated PageEnty<CompanyPersonalListVO> companyPersonalListVO) throws Exception {
        PageDTO<CompanyPersonalListDTO> pageDTO = new PageDTO<>();
        if (companyPersonalListVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = companyPersonalService.countPersonalList(companyPersonalListVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(companyPersonalService.pagePersonalList(companyPersonalListVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "个人列表", pageDTO);

    }

    @PostMapping("/pageAtPersonalList")
    @ApiOperation(value = "考勤组个人列表", notes = "考勤组个人列表")
    public ResultBean<PageDTO<CompanyPersonalListDTO>> pageAtPersonalList(@RequestBody @Validated PageEnty<AttendancePersonalListVO> companyPersonalListVO) throws Exception {
        PageDTO<CompanyPersonalListDTO> pageDTO = new PageDTO<>();
        if (companyPersonalListVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = companyPersonalService.countAttendancePersonalList(companyPersonalListVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(companyPersonalService.pageAttendancePersonalList(companyPersonalListVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "个人列表", pageDTO);

    }

}
